﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 例子 : MonoBehaviour {

    public enum State
    {
        Walk,
        Run,
        Idle,
        Jump,
    }
    private Rigidbody2D rigidbody2d;
    private Animator animator;
    private float V;
    private float H;
    private State state;
    private float CurrentSpeed = 10.0f;
    private bool FacingRight = true;
    private Transform Ground;
    private bool OnGround;
    private bool Jump;

    //public
    public float MoveSpeed;
    public float RunSpeed;
    public float JumpForce;
    // Use this for initialization
    void Start()
    {
        Ground = transform.Find("Ground");
        rigidbody2d = this.GetComponent<Rigidbody2D>();
        animator = this.GetComponent<Animator>();
        FacingRight = true;//保证初始状态所面朝的方向为true方向


    }

    // Update is called once per frame
    void Update()
    {
        OnGround = Physics2D.Linecast(transform.position, Ground.position, 1 << LayerMask.NameToLayer("Ground"));
        V = Input.GetAxis("Vertical");
        H = Input.GetAxis("Horizontal");
        if (Input.GetKeyDown(KeyCode.J) && OnGround)
        {
            Jump = true;
        }
        SpeedAndAnimator();
    }

    void FixedUpdate()
    {
        AnimatorStateInfo animatorState = animator.GetCurrentAnimatorStateInfo(0);
        if (H > 0.3)
        {
            state = State.Walk;
            if (!FacingRight)
                ChangeFacing();
        }
        if (H < -0.3)
        {
            state = State.Walk;
            if (FacingRight)
                ChangeFacing();
        }
        if (Jump)
        {
            state = State.Jump;
            rigidbody2d.gravityScale = 1.0f;
            rigidbody2d.AddForce(new Vector2(0, JumpForce));
            Jump = false;
        }
        if (OnGround)
        {
            rigidbody2d.gravityScale = 0f;
        }
        if (animatorState.IsName("Walk") && animatorState.normalizedTime > 0.1f && Input.GetAxis("Horizontal") > 0.5)
        {
            state = State.Run;
        }
        if (state == State.Walk || state == State.Run && rigidbody2d.velocity.y < 15.0f)
        {
            rigidbody2d.velocity = new Vector2(H * CurrentSpeed, V * CurrentSpeed);
        }
    }
    void ChangeFacing()
    {
        FacingRight = !FacingRight;
        Vector3 theScale = this.transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }

    void SpeedAndAnimator()
    {
        if (state == State.Walk)
        {
            animator.SetInteger("state", 1);
            CurrentSpeed = MoveSpeed;
        }
        else if (state == State.Run)
        {
            animator.SetInteger("state", 2);
            CurrentSpeed = RunSpeed;
        }
        else if (state == State.Idle)
        {
            animator.SetInteger("state", 0);
            CurrentSpeed = 0;
        }
        else if (state == State.Jump)
        {
            //播放跳跃动画
        }
    }
}
